﻿/**********************************************
 * 功能名称：作业成本要素计划价格管理
编写人：  杨莹
建立时间： 杨 2009年9月21日
说明：
管理各作业中心的水、电、空压、冷冻、蒸汽等的计划价格。
 * 价格按月份更新，可编辑。
 **************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.PlanPrice
{
    public partial class CostItems_PlanPriceSetup : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();

        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist(DropDownList_Year, DropDownList_Month, Application["accountPeriod"].ToString());


            }
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
             string MyQueryAccountPeriod = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;
  

            string CurrentUser = Session[ServerVars.sysSessionUserName].ToString();
            CurrentAp.Value = MyQueryAccountPeriod;

            var IsValidString = from a in db.TABC_JS_AccountPeriods
                                where a.会计期间ID == MyQueryAccountPeriod
                                select a;
            if (IsValidString.Count() == 0)
            {
              
                alert = "<script>alert('会计期间不存在，请检查输入是否正确');</script>";
                return;
            }
            else if (IsValidString.Count() == 1)
            {

                MyBindData(MyQueryAccountPeriod);

            }
        }

        private void MyBindData(string Ap)
        {

            var myQueryData = from a in (from h in db.TABC_DM_CostItems
                                         where new string[] { "DF", "SF", "LD", "ZQ", "KY" }.Contains(h.成本细项ID)
                                         select h)
                              join d in (from i in db.TABC_JS_CostItems_PlanPrice where i.会计期间ID == Ap
                                         select i)
                              on a.成本细项ID equals d.成本细项ID
                              into temps
                              from temp in temps.DefaultIfEmpty()
                              orderby a.成本细项ID
                              select new
                              {
                                  ID = (temp==null? 0:temp.ID),
                                  会计期间ID = Ap,
                                  a.成本细项ID,
                                  a.成本细项名称,
                                  计划价 = (temp==null? 0:temp.计划价),


                              };

            GridView_QueryPrice.DataSource = myQueryData;
            GridView_QueryPrice.DataBind();
        
        }

        protected void Button_SavePrice_Command(object sender, CommandEventArgs e)
        {
            string EditCostItemId = e.CommandArgument.ToString();
            string myAp = CurrentAp.Value.ToString();

            var EditRow = from a in db.TABC_JS_CostItems_PlanPrice
                          where a.成本细项ID == EditCostItemId.ToString()
                          && a.会计期间ID == myAp
                          select a;

            string myUser =  Session[ServerVars.sysSessionUserName].ToString();

               
                int RowCount = EditRow.Count();

                if (RowCount == 1)
                {
                    var MyRow = EditRow.Single();
                    GridViewRow myRows = (GridViewRow)((Button)sender).Parent.Parent;
                    string myPrice = ((TextBox)myRows.Cells[3].FindControl("TextBox_PlanPrice")).Text;

                    MyRow.计划价 = Convert.ToDecimal(myPrice);
                    MyRow.操作员ID = myUser;

                                   try
                                     {                   
                                       db.SubmitChanges();                  
                                       alert = "<script>alert('保存成功');</script>";

                                       MyBindData(myAp);
                                   }               
                                   catch              
                                   {                    
                                       alert = "<script>alert('保存失败');</script>";               
                                   }


                }else if(RowCount == 0)
                {

                    TABC_JS_CostItems_PlanPrice AddRow = new TABC_JS_CostItems_PlanPrice();

                    GridViewRow myRows = (GridViewRow)((Button)sender).Parent.Parent;
                    string myPrice = ((TextBox)myRows.Cells[3].FindControl("TextBox_PlanPrice")).Text;
                    AddRow.成本细项ID = EditCostItemId;
                    AddRow.会计期间ID = myAp;
                    AddRow.操作员ID = myUser;
                    AddRow.计划价 = Convert.ToDecimal(myPrice);

                    db.TABC_JS_CostItems_PlanPrice.InsertOnSubmit(AddRow);

                                   try
                {
                    db.SubmitChanges();
                    alert = "<script>alert('保存成功');</script>";
                    MyBindData(myAp);
                }
                catch
                {
                    alert = "<script>alert('保存失败');</script>";
                }

                
                
            
            }
 


        }

 
           
    }
}
